﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ClassLibrary.Data;
using ClassLibrary.Entity;
using System.Data;

namespace ClassLibrary.DAO
{
    public class TblUserGuideDAO
    {
        private DBService _db = null;
        public TblUserGuideDAO(string ConnectionString)
        {
            _db = new DBService(ConnectionString);
        }
        public bool InsUpdUserGuide(TblUserGuide UserGuide, int mode)
        {
            bool flag = false;
            try
            {
                _db.ExeSP_NonQuery("sp_TblUserGuide_InsUpd",
                    new string[] { "@IDUserGuide", "@Title", "@ImageURL", "@ShortDescription", "@DescriptionDetails", "@Display", "@IdLanguage", "@mode" },
                    new object[] { UserGuide.IDUserGuide, UserGuide.Title, UserGuide.ImageURL, UserGuide.ShortDescription, UserGuide.DescriptionDetails, UserGuide.Display, UserGuide.IdLanguage, mode},
                    CommandType.StoredProcedure);
                flag = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }

        public DataTable GetListUserGuide()
        {   
            DataTable dt = null;
            try {
                dt = _db.ExeSP_DataTable("select * from TblUserGuide", false, CommandType.Text);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        public DataTable GetTop3ListUserGuide()
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("select top 3 * from TblUserGuide order by IDUserGuide desc", false, CommandType.Text);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        public TblUserGuide GetUserGuideById(int IDUserGuide)
        {
            TblUserGuide userguide = new TblUserGuide();
            try 
            {
                DataTable dt = _db.ExeSP_DataTable("select * from TblUserGuide where IDUserGuide = @IDUserGuide",
                    new string[] { "@IDUserGuide" },
                    new object[] { IDUserGuide},
                    CommandType.Text);
                foreach (DataRow dr in dt.Rows)
                {
                    userguide.IDUserGuide = IDUserGuide;
                    userguide.Title = dr["Title"].ToString();
                    userguide.ImageURL = dr["ImageURL"].ToString();
                    userguide.ShortDescription = dr["ShortDescription"].ToString();
                    userguide.DescriptionDetails = dr["DescriptionDetails"].ToString();
                    userguide.Display = Convert.ToBoolean(dr["Display"]);
                    userguide.IdLanguage = Convert.ToInt32(dr["IdLanguage"]);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return userguide;
        }
    }
}
